package com.heima.search.interceptor;

import com.heima.common.dto.User;
import com.heima.common.dto.UserThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @Name : UserInterceptor
 * @Author : WuQiang
 * @Date : 2022-10-09 18:43
 * @Description : 登录拦截器
 */
public class UserInterceptor implements HandlerInterceptor {
    @Override //进入controller方法之前
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        String userName = request.getHeader("userName");
        if (StringUtils.isNotBlank(userId)) {
            User user = new User();
            user.setUserId(Integer.valueOf(userId));
            user.setName(userName);
            UserThreadLocalUtil.set(user);
        }
        return true;
    }

    @Override //从controller方法出来
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
       UserThreadLocalUtil.remove();
    }

  /*  @Override  //完成jsp页面内容渲染
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }*/
}
